﻿var responses_by_question_num = new Array();
var Q_ID = 0;
var Q_TEXT = 1;
var Q_TYPE = 2;
var Q_RESP_1 = 3;

function drawPieCharts() {
    for (var i = 0; i < responses_by_question_num.length; i++) {
        if (responses_by_question_num[i][Q_TYPE] == 3) { //3=T/F type of question
            //Go through and count up all the T/F responses
            var trueResponses = 0;
            var falseResponses = 0;
            var t = "True";
            for (var j = Q_RESP_1; j < responses_by_question_num[i].length; j++) {
                if (responses_by_question_num[i][j] == "True") {
                    trueResponses = trueResponses + 1;
                } else {
                    falseResponses = falseResponses + 1;
                }
            }
            // Create the data table.
            var data = new google.visualization.DataTable();
            data.addColumn('string', 'T/Fs');
            data.addColumn('number', 'Num Responses');
            data.addRows([
		        ['True', trueResponses],
		        ['False', falseResponses]
	        ]);

            // Set chart options
            var options = {
                'width': 400,
                'height': 300
            };

            var div = document.getElementById('chart_div_' + responses_by_question_num[i][Q_ID])

            var heading = document.createElement("h5");
            heading.appendChild(document.createTextNode(responses_by_question_num[i][Q_TEXT]));
            div.appendChild(heading);

            var sub_div = document.createElement("div");
            div.appendChild(sub_div);

            // Instantiate and draw our chart, passing in some options.
            var chart = new google.visualization.PieChart(sub_div);
            chart.draw(data, options);
        }

    }
    
    
						
}

function drawBarCharts() {
    for (var i = 0; i < responses_by_question_num.length; i++) {
        if (responses_by_question_num[i][Q_TYPE] == 2) { //2=mult choice type of question
            //Go through and count up all the responses dynamically
            // responses variable is as follows
            //
            //      [     0    ][        1      ]
            // [0]  [ response ][ num responses ]
            // [1]  [ response ][ num responses ]
            //  .
            //  .
            //  .
            var responses = new Array();
            for (var j = Q_RESP_1; j < responses_by_question_num[i].length; j++) {
                var found = false;
                for(var k = 0; k<responses.length; k++){
                    if (responses_by_question_num[i][j] === responses[k][Q_ID]) {
                        found = true;
                        responses[k][1]++;
                    }
                }
                //If we can't find the response in the list, add it to the list
                if (!found) {
                    var newArr = new Array();
                    newArr.push(responses_by_question_num[i][j]);
                    newArr.push(1);
                    responses.push(newArr);

                }
            }

            // Create the data table.
            var data = new google.visualization.DataTable();
            data.addColumn('string', 'Question ' + responses_by_question_num[i][Q_ID]);
            data.addColumn('number', 'Num Responses');
            for (var k = 0; k < responses.length; k++) {
                data.addRow(
		            [responses[k][0], responses[k][1]]
	            );
            }

            // Set chart options
            var options = {
                'width': 400,
                'height': 300
            };

            var div = document.getElementById('chart_div_' + responses_by_question_num[i][Q_ID])

            var heading = document.createElement("h5");
            heading.appendChild(document.createTextNode(responses_by_question_num[i][Q_TEXT]));
            div.appendChild(heading);

            var sub_div = document.createElement("div");
            div.appendChild(sub_div);

            // Instantiate and draw our chart, passing in some options.
            var chart = new google.visualization.ColumnChart(sub_div);
            chart.draw(data, options);
        }
    }
}


function drawTables() {
    for (var i = 0; i < responses_by_question_num.length; i++) {
        if (responses_by_question_num[i][Q_TYPE] == 1) { //1=text response
            var div = document.getElementById('chart_div_' + responses_by_question_num[i][Q_ID]);
            
            var heading = document.createElement("h5");
            heading.appendChild(document.createTextNode(responses_by_question_num[i][Q_TEXT]));
            div.appendChild(heading);
            var table = document.createElement("table");
            table.setAttribute("id", "q" + responses_by_question_num[i][Q_ID]);
            table.setAttribute("class", "table table-condensed");

            var headerRow = document.createElement("thead");
            var row = document.createElement("tr");
            
            var number = document.createElement("th");
            number.appendChild(document.createTextNode("Number"));
            row.appendChild(number);

            var responses = document.createElement("th");
            responses.appendChild(document.createTextNode("Responses"));
            row.appendChild(responses);

            headerRow.appendChild(row);
            table.appendChild(headerRow);
            
            var bodyTag = document.createElement("tbody");
            bodyTag.setAttribute("id", "tbody" + responses_by_question_num[i][Q_ID]);
            table.appendChild(bodyTag);

            for (var j = Q_RESP_1; j < responses_by_question_num[i].length; j++) {
                var row = document.createElement("tr");
                var numCol = document.createElement("td");
                numCol.appendChild(document.createTextNode(j - 1));
                var respCol = document.createElement("td");
                respCol.appendChild(document.createTextNode(responses_by_question_num[i][j]));
                row.appendChild(numCol);
                row.appendChild(respCol);
                table.appendChild(row);
            }
            
            div.appendChild(table);
        }

    }

}

//This function fills out the responses_by_question_num field as follows
//
//      [     0     ][      1      ][   2  ][     3    ][     4    ]...
// [0]  [question id][question text][ type ][response 1][response 2]...
// [1]  [question id][question text][ type ][response 1][response 2]...
//  .
//  .
//  .
function addResponse(questionId, questionText, type, response) {
    var found = false;
    for (var i = 0; i < responses_by_question_num.length; i++) {
        if (responses_by_question_num[i][Q_ID] == questionId) {
            responses_by_question_num[i].push(response);
            found = true;
        }

    }
    if (!found) {
        var newArr = new Array();
        newArr.push(questionId);
        newArr.push(questionText);
        newArr.push(type);
        newArr.push(response);
        var main_div = document.getElementById("responses_div");
        var new_div = document.createElement("div");
        new_div.setAttribute("id", "chart_div_" + questionId);
        new_div.setAttribute("style", "width:400; height:300");
        new_div.setAttribute("class", "well");
        main_div.appendChild(new_div);
        responses_by_question_num.push(newArr);
    }
}